﻿"use strict";
const mvc = require('cosjs.mvc');
const cosjs = require('cosjs');

const _gzone = mvc.config.get("gzone");
const _dbPool = cosjs.pool;
const _branch = mvc.config.get("branch");
const _dbname = mvc.config.get("dbname") || _branch['id'];
const _redis = mvc.library.require('redis');
const _mongodb = mvc.library.require('mongodb');

exports.gzone = {}
//公共MONGODB
exports.mongodb = class mongodb extends _mongodb {
    constructor(sid, name, upsert) {
        let id = _gzone ? sid : 0;
        let pkey = ["mongodb", id].join('');
        let dbname = [_dbname, id].join('-');

        let mongo = _dbPool.get(pkey);
        if (!mongo) {
            throw new Error(`dbase mongodb[${pkey}] empty`)
        }
        super(mongo, dbname, name, upsert);
    }
}
//公共REDIS
exports.redis = class redis extends _redis.hash {
    constructor(sid, name, format) {
        let id = _gzone ? sid : 0;
        let pkey = ["redis", id].join('');
        let prefix = [_dbname, 'R', id, name].join('-');

        let redis = _dbPool.get(pkey);
        if (!redis) {
            throw new Error(`dbase redis[${pkey}] empty`)
        }
        let formatOpt = format ? mvc.format(format) : null;
        super(redis, prefix, formatOpt);
    }
}

//set
exports.set = class set extends _redis.set {
    constructor(sid, name) {
        let id = _gzone ? sid : 0;
        let pkey = ["redis", id].join('');
        let prefix = [_dbname, 'S', id, name].join('-');

        let redis = _dbPool.get(pkey);
        if (!redis) {
            throw new Error(`dbase set[${pkey}] empty`)
        }
        super(redis, prefix);
    }
}


//排行榜
exports.sort = class sort extends _redis.sort {
    constructor(sid, name) {
        let id = _gzone ? sid : 0;
        let pkey = ["redis", id].join('');
        let prefix = [_dbname, 'Z', id, name].join('-');

        let redis = _dbPool.get(pkey);
        if (!redis) {
            throw new Error(`dbase zset[${pkey}] empty`)
        }
        super(redis, prefix);
    }
}


//用户锁
exports.gzone.locks = class gzone_locks extends _redis.locks {
    constructor(sid, name) {
        let pkey = ["cache", sid].join("");
        let redis = _dbPool.get(pkey);
        if (!redis) {
            throw new Error(`dbase gzone.lock[${pkey}] empty`)
        }
        let prefix = [_dbname, 'L', sid, name].join('-');
        super(redis, prefix);
    }
}
//基础CACHE
exports.gzone.cache = class gzone_cache extends _redis.hash {
    constructor(sid, name, format) {
        let pkey = ["cache", sid].join("");
        let redis = _dbPool.get(pkey);
        if (!redis) {
            throw new Error(`dbase gzone.cache[${pkey}] empty`)
        }
        let prefix = [_dbname, 'C', sid, name].join('-');
        let formatOpt = format ? mvc.format(format) : null;
        super(redis, prefix, formatOpt);
    }
}


exports.list = class list extends _redis.list {
    constructor(sid, name) {
        let id = _gzone ? sid : 0;
        let pkey = ["redis", id].join('');
        let prefix = [_dbname, 'L', id, name].join('-');

        let redis = _dbPool.get(pkey);
        if (!redis) {
            throw new Error(`dbase zset[${pkey}] empty`)
        }
        super(redis, prefix);
    }
}



//基础mongodb
exports.gzone.mongodb = class gzone_mongodb extends _mongodb {
    constructor(sid, name, upsert) {
        let pkey = ["mongodb", sid].join("");
        let mongo = _dbPool.get(pkey);
        if (!mongo) {
            throw new Error(`dbase gzone.mongodb[${pkey}] empty`)
        }
        let dbname = [_dbname, sid].join('-');
        super(mongo, dbname, name, upsert);
    }
}
